<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/26
 * Time: 14:14
 */

/**
 * Class Involved_UserModel
 */

class Involved_UserModel extends BaseModel{

    protected $table = 'user';

    /**
     * 根据用户id更新用户信息
     * @param int $user_id
     * @param array $data
     * @param mixed
     */
    public function updateByUserId($user_id, $data) 
    {
        return DB::table($this->table)->where(['user_id' => $user_id])->update($data);
   	}

    /**
     * 投资记录
     * @return mixed
     * @author lzhp
     * @date 2016-05-03
     */
    public function UserInvestTicket($user_id)
    {
        return DB::table($this->table)
            ->select('project_name','invest_money','invest_time','yield_rate','limit','yield_money','invest_record.status','invest_record.user_id','user_name')
            ->join('invest_record','invest_record.user_id',' = ','user.user_id')
            ->where('user.user_id',$user_id)
            ->paginate(10);

    }

    /**
     * 报票记录
     * @return mixed
     * @author lzhp
     * @date 2016-05-03
     */
    public function UserBTicket($user_id)
    {
        return DB::table($this->table)
            ->join('ticket_record','ticket_record.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->paginate(10);
    }

    /**
     * 登陆验证
     * @param $name
     * @param $pwd
     * @return mixed
     */
    public function getUserInfo($name,$pwd)
    {
        return DB::table($this->table)
            ->join('user_extend','user_extend.user_id','=','user.user_id')
            ->where(['mobile'=>$name,'password'=>$pwd])
            ->orwhere(['user_name'=>$name,'password'=>$pwd])
            ->first();
    }

    /**
     * js登陆验证用户名
     * @param $name
     * @return mixed
     */
    public function JsCheckUser($name)
    {
        return DB::table($this->table)
            ->select('user_name')
            ->where('user_name',$name)
            ->orwhere('mobile',$name)
            ->first();
    }

    /**
     * js登陆验证密码
     * @param $name
     * @return mixed
     */
    public function JsCheckPwd($name,$pwd)
    {
        return DB::table($this->table)
            ->select('password')
            ->where(['user_name'=>$name,'password'=>$pwd])
            ->orwhere(['mobile'=>$name,'password'=>$pwd])
            ->first();
    }
    /**
     * js注册验证用户名
     * @param $name
     * @return mixed
     */
    public function JsCheckUsername($name)
    {
        return DB::table($this->table)
            ->select('user_name')
            ->where('user_name',$name)
            ->first();
    }

    /**
     * js注册验证手机号
     * @param $mobile
     * @return mixed
     */
    public function JsCheckMobile($mobile)
    {
        return DB::table($this->table)
            ->select('mobile')
            ->where('mobile',$mobile)
            ->first();
    }

    /**
     * js注册验证邀请人手机号
     * @param $invite
     * @return mixed
     */
    public function JsCheckInvite($invite)
    {
        return DB::table($this->table)
            ->select('invite_mobile')
            ->where('mobile',$invite)
            ->first();
    }

    //账户明细
    public function Account($user_id)
    {
        return DB::table($this->table)
            ->join('account_record','account_record.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->paginate(10);

    }

    //充值
    public function Recharge($user_id)
    {
        return DB::table($this->table)
            ->join('recharge_record','recharge_record.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->paginate(10);

    }

    //提现
    public function WithDraw($user_id)
    {
        return DB::table($this->table)
            ->join('withdraw_record','withdraw_record.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->paginate(10);
    }

    public function Read($user_id)
    {
        return DB::table($this->table)
            ->join('message','message.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id,'message.status'=>1])
            ->get();
    }
//    public function ReadPage($user_id,$limit,$page_size)
//    {
//        return DB::table($this->table)
//            ->join('message','message.user_id','=','user.user_id')
//            ->where(['user.user_id'=>$user_id,'message.status'=>1])
//            ->limit($limit,$page_size)
//            ->get();
//    }

    public function UnRead($user_id)
    {
        return DB::table($this->table)
            ->join('message','message.user_id','=','user.user_id')
            ->where(['user.user_id'=>$user_id,'message.status'=>2])
            ->get();
    }

    public function ReadAll($user_id)
    {
        return DB::table($this->table)
            ->join('message','message.user_id','=','user.user_id')
            ->where('user.user_id',$user_id)
            ->get();
    }

    /**
     * 删除多条消息
     * @param $num
     * @return mixed
     */
    public function UserNewsDel($num)
    {
        return DB::delete("delete from ypt_message where message_id in ($num)");
    }

    /**
     * 实名认证
     * @param $id_card
     * @return mixed
     */
    public function CheckCard($id_card)
    {
        return DB::table($this->table)
            ->where('id_card',$id_card)
            ->first();
    }

    /**
     * 实名认证状态
     * @param $user_id
     * @return mixed
     */
    public function CheckCardStatus($user_id)
    {
        return DB::table($this->table)
            ->select('status')
            ->where('user_id',$user_id)
            ->first();
    }

    /**
     * 忘记密码手机验证
     * @param $mobile
     * @return mixed
     */
    public function JsVerifyUserMobile($mobile)
    {
        return DB::table($this->table)
            ->select('mobile')
            ->where('mobile',$mobile)
            ->first();
    }

    public function UserName($user_id)
    {
        return DB::table($this->table)
            ->select('user_name','user_id')
            ->whereIn('user.user_id',$user_id)
            ->get();
    }


}